package com.juli.school.service.impl;

import java.util.UUID;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.juli.school.dao.entity.Stu;
import com.juli.school.dao.mapper.StuMapper;
import com.juli.school.service.bo.LoginBo;
import com.juli.school.service.bo.StuBo;
import com.juli.school.service.utils.BeanUtils;
import com.juli.school.utils.core.model.CommonResp;

/**
 * @Description: 登录服务接口
 * @author gaoxiang
 * @date 2018年1月8日 上午11:51:11
 */ 
@Service
public class StuService {
	
	private final static Logger logger = LoggerFactory.getLogger(StuService.class);
	
	@Resource
	StuMapper stuMapper;
	
	public CommonResp<Stu> saveStu(LoginBo loginBo) {
		CommonResp<Stu> commonResp = new CommonResp<Stu>();
		try {
			String stuId = UUID.randomUUID().toString();
			Stu stu = new Stu();
			stu.setStuId(stuId);
			stu.setStuNo(loginBo.getStuNo());
			stu.setPhone(loginBo.getPhone());
			stu.setIsActive("Y");
			stuMapper.insert(stu);
			Stu stuResp = stuMapper.getByStuId(stu.getStuId());
			if(stuResp == null) {
				commonResp.fail("保存学生信息失败");
				return commonResp;
			}
			commonResp.setData(stuResp);
			commonResp.success();
		} catch (Exception e) {
			logger.error("保存学生信息发生异常",e);
			commonResp.exception("保存学生信息发生异常:"+e.toString());
		}
		return commonResp;
	}

	public CommonResp<String> updateStu(StuBo stuBo) {
		CommonResp<String> commonResp = new CommonResp<String>();
		try {
			Stu stu = stuMapper.getByStuId(stuBo.getStuId());
			BeanUtils.copyProperties(stuBo, stu);
			stuMapper.update(stuBo);
			commonResp.success();
		}catch (Exception e) {
			logger.error("更新学生资料信息异常", e);
			commonResp.exception("更新学生资料信息异常 " + e.getMessage());
		}
		return commonResp;
	}
}
